import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_common_app/pages/empty_page.dart';
import 'package:flutter_common_app/router/route_handlers.dart';
import 'package:flutter_common_app/utils/common_util/log_util.dart';

class Routes {
  static FluroRouter router;

  static String root = '/';
  static String indexPage = '/indexPage';
  static String login = '/login';
  static String playlistDetailPage = '/playlistDetailPage';
  static String playingDetailPage = '/playingDetailPage';
  static String recommendDailySongsPage = '/recommendDailySongsPage';
  static String topListPage = '/topListPage';
  static String songCalendarDetailPage = '/songCalendarDetailPage';
  static String webViewPage = '/webViewPage';

  static void configureRoutes(FluroRouter router) {
    router.notFoundHandler = Handler(
        handlerFunc: (BuildContext context, Map<String, List<String>> params) {
      return EmptyPage();
    });

    router
      ..define(root, handler: rootHandler)
      ..define(indexPage, handler: indexHandler)
      ..define(login, handler: loginHandler)
      ..define(playlistDetailPage, handler: playlistDetailHandler)
      ..define(playingDetailPage, handler: playingDetailHandler)
      ..define(recommendDailySongsPage, handler: recommendDailySongsHandler)
      ..define(topListPage, handler: topListHandler)
      ..define(songCalendarDetailPage, handler: songCalendarDetailHandler)
      ..define(webViewPage, handler: webViewHandler);
  }

  static navigateTo(
    BuildContext context,
    String path, {
    Map<String, dynamic> params,
    TransitionType transitionType = TransitionType.cupertino,
    bool isReplace = false,
  }) {
    String query = '';
    if (params != null) {
      int index = 0;
      for (var key in params.keys) {
        var value = Uri.encodeComponent(params[key].toString());
        if (index == 0) {
          query += '?';
        } else {
          query += '\&';
        }
        query += '$key=$value';
        index++;
      }
    }

    LogUtil.v('navigateTo 参数->: $query');

    path += query;

    return router.navigateTo(
      context,
      path,
      transition: transitionType,
      replace: isReplace,
    );
  }

  static pop(BuildContext context) {
    router.pop(context);
  }
}
